package net.hn.hnms.biz.meeting.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.meeting.domain.MeetingInfo;
import net.hn.hnms.biz.meeting.request.MeetingInfoReq;
import net.hn.hnms.biz.meeting.vo.MeetingInfoVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface MeetingInfoMapper extends BaseMapper<MeetingInfo> {



    @Results(
            value = {
                    @Result(property = "meetingPersonId" ,column = "meeting_person_id"),
                    @Result(property = "personId" ,column = "person_id"),
                    @Result(property = "meetingId",column = "meeting_id"),
                    @Result(property = "personName",column = "person_name"),
                    @Result(property = "MeetingPersonList" ,column = "meeting_id" ,many = @Many(select = "net.hn.hnms.biz.meeting.mapper.MeetingPersonMapper.selectListSq"))
            }
    )
    @Select("<script>" +
//            "  select meeting_id from meeting_info "+
            "select mi.meeting_id,mi.meeting_date,mi.start_time,mi.end_time,mi.meeting_name , " +
            "               mi.meeting_sponsor ,mr.meeting_room_name,mr.meeting_room_id, " +
            "               mr.meeting_room_type,mr.meeting_room_address, " +
            "               mi.remarks, mi.status,mi.file_name,mi.file_path,mi.oss_id,mi.create_time " +
//            "               mp.meeting_person_id " +
            " from meeting_info mi " +
            "               inner join meeting_room mr on  mi.meeting_room_id = mr.meeting_room_id " +
//            "               left join meeting_person mp on mi.meeting_id = mp.meeting_id " +
            "        where mi.del_flag='0'  and mr.del_flag = '0' " +
            "            <if test='query.meetingName!=null and query.meetingName != \"\" '>" +
            "                and mi.meeting_name like concat('%',#{query.meetingName},'%') " +
            "            </if>\n" +

//            " <if test='query.waterPlaceName != null and query.waterPlaceName != \"\" '> "+

            "            <if test='query.meetingSponsor!=null and query.meetingSponsor!= \"\"'>\n" +
            "                and mi.meeting_sponsor like concat('%',#{query.meetingSponsor},'%')\n" +
            "            </if>\n" +
            "            <if test='query.meetingRoomId!=null and query.meetingRoomId != \"\"'>\n" +
            "                and mi.meeting_room_id=#{query.meetingRoomId}\n" +
            "            </if>\n" +
            "\n" +
            "            <if test='query.startTime!=null '>\n" +
//            "                and mi.start_time <![CDATA[ >= ]]> to_timestamp( #{query.startTime}, 'yyyy-MM-dd hh24:mi:ss' )\n" +
            "                and mi.start_time <![CDATA[ >= ]]> to_timestamp( #{query.startTime}, 'yyyy-MM-dd hh24:mi:ss' )\n" +
            "            </if>\n" +
            "            <if test='query.endTime!=null ' >\n" +
            "                AND mi.end_time <![CDATA[ <= ]]> to_timestamp( #{query.endTime}, 'yyyy-MM-dd hh24:mi:ss' )\n" +
            "            </if>\n" +
            "            <if test='query.meetingDate!=null ' >\n" +
            "                AND mi.meeting_date  =   #{query.meetingDate,jdbcType=DATE } \n" +
            "            </if>\n" +
            "            <if test='query.status!=null and query.status != \"\"' >\n" +
            "                AND mi.status  =   #{query.status } \n" +
            "            </if>\n" +
            "            <if test='query.mineCode!=null and query.mineCode != \"\" and query.companyCode!= null and query.companyCode != \"\" ' >\n" +
            "                AND (mi.mine_code  =   #{query.mineCode } or mi.company_code = #{query.companyCode}) \n" +
            "            </if>\n" +
            "            <if test='query.mineCode!=null and query.mineCode != \"\"  and query.companyCode!= null and query.companyCode != \"\" ' >\n" +
            "                AND (mr.mine_code  =   #{query.mineCode } or mr.company_code = #{query.companyCode} )\n" +
            "            </if>\n" +

            "\n" +
            "            order by mi.status ,mi.meeting_date,mi.start_time " +
            "</script>")
    Page<MeetingInfoVo> selectPageListSql(Page<Object> build, @Param("query") MeetingInfoReq meetingInfoReq);



    @Update("<script>" +
            " update  hygl.meeting_info set del_flag = '1' " +
            " where meeting_id in" +
            " <foreach collection=\"list\" item=\"value\" separator=\",\" open=\"(\" close=\")\">\n" +
            "#{value}" +
            "</foreach>" +
            "</script>")
    void updateDeleteByIds(@Param("list") List<String> list);


    @Select("<script>"+"select meeting_name from meeting_info where meeting_room_id=#{deptid.meetingRoomId} and status != '2' and del_flag = '0' " +
            " and ((( "+
            " ( "+
            " end_time <![CDATA[ > ]]> to_timestamp( #{deptid.startTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " and start_time <![CDATA[ < ]]> to_timestamp( #{deptid.startTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " ) or  ( "+
            " end_time <![CDATA[ > ]]> to_timestamp( #{deptid.endTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " and start_time <![CDATA[ < ]]> to_timestamp( #{deptid.endTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " )) )or ((end_time <![CDATA[ < ]]> to_timestamp( #{deptid.endTime}, 'yyyy-MM-dd hh24:mi:ss' ) and start_time <![CDATA[ > ]]> to_timestamp( #{deptid.startTime}, 'yyyy-MM-dd hh24:mi:ss' ))))"+
            "</script>")
    List<MeetingInfoVo> selectMeetingInfoList(@Param("deptid") MeetingInfoReq meetingInfoReq);

//    @Select("<script>"+"select meeting_name from meeting_info where meeting_room_id=#{deptid.meetingRoomId} and meeting_id !=#{deptid.meetingId} and status != '2' and end_time <![CDATA[ > ]]> to_timestamp( #{deptid.startTime}, 'yyyy-MM-dd hh24:mi:ss' )"+"</script>")
    @Select("<script>"+"select meeting_name from meeting_info where meeting_room_id=#{deptid.meetingRoomId} and meeting_id !=#{deptid.meetingId} and status != '2' and del_flag = '0' " +
            " and ((( "+
            " ( "+
            " end_time <![CDATA[ > ]]> to_timestamp( #{deptid.startTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " and start_time <![CDATA[ < ]]> to_timestamp( #{deptid.startTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " ) or  ( "+
            " end_time <![CDATA[ > ]]> to_timestamp( #{deptid.endTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " and start_time <![CDATA[ < ]]> to_timestamp( #{deptid.endTime}, 'yyyy-MM-dd hh24:mi:ss' )"+
            " )) )or ((end_time <![CDATA[ < ]]> to_timestamp( #{deptid.endTime}, 'yyyy-MM-dd hh24:mi:ss' ) and start_time <![CDATA[ > ]]> to_timestamp( #{deptid.startTime}, 'yyyy-MM-dd hh24:mi:ss' ))))"+
            "</script>")
    List<MeetingInfoVo> selectMeetingInfoListForUpd(@Param("deptid") MeetingInfoReq meetingInfoReq);



}
